package com.example.demo.controller;


import com.example.demo.entity.Role;
import com.example.demo.service.IRoleService;
import com.example.demo.utils.AjaxJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author 
 * @since 2025-11-10
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    
    @Autowired
    private IRoleService roleService;
    
    @RequestMapping("/add")
    public AjaxJson add(@RequestBody Role role) {
        return roleService.save(role) ? AjaxJson.getSuccess("添加成功") : AjaxJson.getError("添加失败");
    }
    
    @RequestMapping("/delete")
    public AjaxJson delete(@RequestParam Integer id) {
        return roleService.removeById(id) ? AjaxJson.getSuccess("删除成功") : AjaxJson.getError("删除失败");
    }
    
    @RequestMapping("/update")
    public AjaxJson update(@RequestBody Role role) {
        return roleService.updateById(role) ? AjaxJson.getSuccess("修改成功") : AjaxJson.getError("修改失败");
    }
    
    @RequestMapping("/query")
    public AjaxJson query() {
        return AjaxJson.getPageData(roleService.count(), roleService.list());
    }
    
    @RequestMapping("/query/id")
    public AjaxJson query(@RequestParam Integer id) {
        return AjaxJson.getSuccessData(roleService.getById(id));
    }
}